package com.freshcity.page.feiji
{
	import com.freshcity.component.alert.Alert;
	import com.freshcity.component.container.Component;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.feiji.FeiJiViewWin;
	import com.freshcity.feiji.NoFeijiBack;
	import com.freshcity.feiji.ReceiveFeijMsg;
	import com.freshcity.feiji.SearchFeijiBack;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.page.common.UserDetail;
	import com.freshcity.page.common.UserHome;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class FeiJiView extends Component
	{
		public var selectedPost:*;

		public var feijiInfo:FeiJiInfo;
		public var searchFeiji:SearchFeijiBack;
		private var popMessage:ReceiveFeijMsg;
		private var noFeiji:NoFeijiBack;
		private var addFeijiLink:TextField;
		private var back:FeiJiViewWin;

		public function FeiJiView()
		{
			this.visible=false;
			super(null,0,0);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new FeiJiViewWin();
			addChild(back);
			
			popMessage=new ReceiveFeijMsg();
			popMessage.closeBtn.addEventListener(MouseEvent.CLICK, onClosePopWin);
			popMessage.submitBtn.addEventListener(MouseEvent.CLICK, onClosePopWin);
			
			feijiInfo=new FeiJiInfo();
			feijiInfo.owner=this;
			back.feijiList.addChild(feijiInfo);
			
			noFeiji = new NoFeijiBack();
			back.feijiList.addChild(noFeiji);
			
			searchFeiji = new SearchFeijiBack();
			back.feijiList.addChild(searchFeiji);
			
			addFeijiLink=StringUtil.getTextField("自己去投个纸飞机吧！");
			addFeijiLink.y=120;
			noFeiji.addChild(addFeijiLink);
			
			this.initlize();			
		}
		
		override public function draw():void
		{
			super.draw();
			this.main.commu.register(OpCodes.EVALUATE_FEIJI, onEvaluteFeiji);
			this.main.commu.register(OpCodes.READ_FEIJI, renderContent);
		}
		
		private function initlize():void
		{
			this.noFeiji.visible=false;
			this.noFeiji.y = -40;
			
			this.feijiInfo.visible=false;
			this.searchFeiji.visible=false;
			this.addFeijiLink.visible=false;
			this.selectedPost = null;
		}		

		protected function onClosePopWin(event:MouseEvent):void
		{
			var myfun:Function=function():void
			{
				EffectManager.hideUIMask();
				LayerManager.highestLayer.removeChild(popMessage);
				search();
			};
			TweenLite.to(popMessage, 0.3, {alpha: 0, y: "+50", onComplete: myfun});
		}


		public function showUserDetail(event:Event):void
		{
			if (this.selectedPost)
			{
				var from:String=this.selectedPost.fromUser;
				Dispatcher.addEventListener(EventName.UserReadDone,readUserCallback);
				this.main.findUser(from);
			}
		}
		
		private function readUserCallback(event:DataEvent):void
		{
			Dispatcher.removeEventListener(EventName.UserReadDone,readUserCallback);
			EffectManager.hideLoadMask();
			var data:* = event.data;
			MainScene.userInfoHome.data=data;
			//MainScene.userInfoHome.funModel = UserHome.SEND_WORD;
			if(!MainScene.writeMsg.hasEventListener(Event.COMPLETE)){
				MainScene.writeMsg.addEventListener(Event.COMPLETE,onWriteMsgCallback);
			}
		}

		private function onWriteMsgCallback(event:Event):void
		{
			this.feijiInfo.onYesClick();
		}

		public function onReply(event:Event):void
		{
			var timeDesc:String="".concat("<font color=\'#ff0000\' size=\'13\'>", this.selectedPost.expDateStr, "</font>");
			var htmlContent:String="  收取飞机意味着承诺，您确认能在" + timeDesc + "按时赴约吗？";
			var htmlTitle:String="<font color=\'#f5ff00\'>纸飞机</font>";

			if (this.main.user.level < 4)
			{
				this.main.msg.showRollTipsMsg("个人级别升到四级才可以搭乘飞机哦！");
			}else{
				Alert.show(htmlContent, htmlTitle, Alert.OK | Alert.CANCEL, null, finishReply);
			}
		}

		private function finishReply(arg1:int):void
		{
			if (arg1 == Alert.OK)
			{
				var postId:String=this.selectedPost.id;
				var fromUser:String=this.selectedPost.fromUser;
				var createdAt:String=this.selectedPost.createdAt;

				this.main.commu.send(OpCodes.GET_FEIJI, {id: postId, from: fromUser, date: createdAt}, onGetFeiji);
			}
		}

		public function onEvaluteFeiji(data:*):void
		{
			if (data is String)
			{
				feijiInfo.setVoteBtn(true);
				this.main.msg.showRollTipsMsg(data);
			}
			else if (data is int)
			{
				search();
			}
		}
		
		private function search():void
		{
			feijiInfo.visible=false;
			TweenMax.to(searchFeiji,0.5,{autoAlpha:1,onComplete:readFeiji});
		}
		
		public function readFeiji():void
		{
			//读取数据
			if (this.selectedPost)
			{
				this.main.commu.send(OpCodes.READ_FEIJI, {b: this.selectedPost.postIndex});
			}
			else
			{
				this.main.commu.send(OpCodes.READ_FEIJI, {});
			}
		}
		

		public function onGetFeiji(data:*):void
		{
			if (data == true)
			{
				//main.msg.showPopUpMsg("约定已成功创建，请按时赴约。", MsgManager.HELLO, 5);
				this.main.msg.showFixRollMsg("纸飞机搭乘成功。");
				EffectManager.addUIMask([], 0);
				LayerManager.highestLayer.addPopUp(popMessage, true);
				TweenLite.from(popMessage, 0.3, {alpha: 0, y: "-50"});
			}
			else
			{
				//Logger.debug(this, "onGetFeiji", data);
				this.main.msg.showRollTipsMsg(data);
			}
		}

		override public function show(event:Event=null):void
		{
			TweenMax.to(this,0.3,{autoAlpha:1,onComplete:search,onInit:initlize});
		}

		override public function hide(event:Event=null):void
		{
			TweenMax.to(this,0.3,{autoAlpha:0});
		}
		

		public function renderContent(data:*):void
		{
			if (data is String)
			{
				this.main.msg.showRollTipsMsg(String(data));
			}
			else if (data == false)
			{
				var myfun:Function = function():void{
					TweenMax.to(searchFeiji,0.5,{autoAlpha:0});
					TweenMax.to(noFeiji,1,{autoAlpha:1,y:0,delay:0.5,onComplete:showLink});
				};
				TweenLite.delayedCall(1,myfun);
			}
			else
			{
				this.selectedPost=data;
				feijiInfo.feiji = data;
				TweenLite.delayedCall(0.5,feijiInfo.onShow);	
			}
		}
		
		private function showLink():void
		{
			var myfun:Function = function():void{
				addFeijiLink.x = 180;
			};
			TweenMax.to(addFeijiLink,0.5,{autoAlpha:1,x:200,onInit:myfun});
		}
		
		private function addNewFeiji(evt:Event):void
		{
			this.hide(evt);
		}
		
		public function showFeiJi(data:*):void
		{
			feijiInfo.feiji=data;
			feijiInfo.setVoteBtn(true);
		}

		override public function destroy():void
		{
			this.main.commu.send(OpCodes.STOP_READ_FEIJI, {});
			this.parent.removeChild(this);
		}
	}
}
